package com.tebiecloud.goods.server.controller.fegin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.goods.client.model.entity.Goods;
import com.tebiecloud.goods.client.model.entity.GoodsSpecification;
import com.tebiecloud.goods.client.service.IGoodsApiServiceClient;
import com.tebiecloud.goods.server.service.GoodsService;
import com.tebiecloud.goods.server.service.GoodsSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/feign/api")
public class GoodsApiController implements IGoodsApiServiceClient {

    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsSpecificationService goodsSpecificationService;

    @GetMapping("listspecification")
    @Override
    public ResultBody<List<GoodsSpecification>>  selectBySpecIds(@RequestParam("specIds") String specIds) {
        if(StringUtils.isEmpty(specIds))return null;
        String[] strings = specIds.split(",");
        List<Long> specIdList = new ArrayList<Long>();
        for(String str:strings){
            specIdList.add(Long.valueOf(str));
        }
        QueryWrapper<GoodsSpecification> goodsSpecificationQueryWrapper = new QueryWrapper<GoodsSpecification>();
        goodsSpecificationQueryWrapper.in("spec_id",specIdList);
        List<GoodsSpecification> goodsSpecificationList = goodsSpecificationService.list(goodsSpecificationQueryWrapper);
        return ResultBodyUtils.ok(goodsSpecificationList);
    }

    @GetMapping("listgoods")
    @Override
    public ResultBody<List<Goods>> selectByGoodsIds(@RequestParam("goodsIds")String goodsIds) {
        if(StringUtils.isEmpty(goodsIds))return null;
        String[] strings = goodsIds.split(",");
        List<Long> goodsIdlist = new ArrayList<Long>();
        for(String str:strings){
            goodsIdlist.add(Long.valueOf(str));
        }
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<Goods>();
        goodsQueryWrapper.in("goods_id",goodsIdlist);
        List<Goods> goodsList = goodsService.list(goodsQueryWrapper);
        return ResultBodyUtils.ok(goodsList);
    }

    /**
     * 查询商品规格列表
     * @param goodsId
     * @return
     */
    @PostMapping("listgoodsspecification")
    @Override
    public ResultBody<List<GoodsSpecification>> selectGoodsSpecification(Long goodsId) {
        QueryWrapper<GoodsSpecification> goodsSpecificationQueryWrapper = new QueryWrapper<>();
        goodsSpecificationQueryWrapper.eq("goods_id",goodsId);
        goodsSpecificationQueryWrapper.eq("deleted",0);
        List<GoodsSpecification> goodsSpecificationList = goodsSpecificationService.list(goodsSpecificationQueryWrapper);
        return ResultBodyUtils.ok(goodsSpecificationList);
    }
}
